package com.interest.service;

import java.util.Timer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean;
import org.springframework.scheduling.support.MethodInvokingRunnable;

import com.alibaba.fastjson.JSONObject;
import com.interest.bean.AccessToken;
import com.interest.bean.menu.Button;
import com.interest.bean.menu.CommonButton;
import com.interest.bean.menu.ComplexButton;
import com.interest.bean.menu.Menu;
import com.interest.time.MyTimerTask;
import com.interest.util.HttpConUtil;

public class MenuSerivce extends Timer{
	private static Logger logger = LoggerFactory.getLogger(MenuSerivce.class);
	private static String MENUCREATEURL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=#ACCESS_TOKEN";

	public static void main(String[] args) {
		// AccessToken accessToken = SignUtil.getAccessToken(appId, appSecret);
		// String token = accessToken.getToken();
		// System.out.println(token);
		int code = createMenu(getMenu(),
				"wIF1qIatGmin8wrhgP8pg7RZS1wKiTujZzxFQMNvTybwFMtsfEn1Zi3kpO_yicMiYH2uFtKC8PR2E29R2Xycb-NqSxaDnlJXvm1sUbARfKv9NkcIgBBpKZBse2t3OqrbBLTeAJAQAI");
		System.out.println(code);
		/*
		 * Menu menu = getMenu();
		 * System.out.println(JSONObject.toJSONString(menu));
		 */
	}

	/**
	 * 创建菜单
	 * 
	 * @param menu
	 * @param accessToken
	 * @return
	 */
	public static int createMenu(Menu menu, String accessToken) {
		int code = -1;
		String url = MENUCREATEURL.replace("#ACCESS_TOKEN", accessToken);
		String content = JSONObject.toJSONString(menu);
		logger.info("ment:{}", content);
		String resp = HttpConUtil.doPost(url, content);
		if (null != resp && !"".endsWith(resp)) {
			JSONObject respJson = JSONObject.parseObject(resp);
			logger.info("创建菜单响应：{}", respJson);
			code = respJson.getIntValue("errcode");
		}
		return code;
	}

	public static Menu getMenu() {
		CommonButton btn11 = new CommonButton();
		btn11.setName("搜索");
		btn11.setType("view");
		btn11.setUrl("http://www.soso.com/");

		CommonButton btn12 = new CommonButton();
		btn12.setName("视频");
		btn12.setType("view");
		btn12.setUrl("http://v.qq.com/");

		CommonButton btn13 = new CommonButton();
		btn13.setName("暂一下");
		btn13.setType("click");
		// btn13.setUrl("http://www.baidu.com/");
		btn13.setKey("V1001_GOOD");

		CommonButton btn14 = new CommonButton();
		btn14.setName("历史上的今天");
		btn14.setType("click");
		btn14.setKey("14");

		CommonButton btn21 = new CommonButton();
		btn21.setName("歌曲点播");
		btn21.setType("click");
		btn21.setKey("V1001_TODAY_MUSIC");

		CommonButton btn22 = new CommonButton();
		btn22.setName("经典游戏");
		btn22.setType("click");
		btn22.setKey("22");

		CommonButton btn23 = new CommonButton();
		btn23.setName("美女电台");
		btn23.setType("click");
		btn23.setKey("23");

		CommonButton btn24 = new CommonButton();
		btn24.setName("人脸识别");
		btn24.setType("click");
		btn24.setKey("24");

		CommonButton btn25 = new CommonButton();
		btn25.setName("聊天唠嗑");
		btn25.setType("click");
		btn25.setKey("25");

		CommonButton btn31 = new CommonButton();
		btn31.setName("Q友圈");
		btn31.setType("click");
		btn31.setKey("31");

		CommonButton btn32 = new CommonButton();
		btn32.setName("电影排行榜");
		btn32.setType("click");
		btn32.setKey("32");

		CommonButton btn33 = new CommonButton();
		btn33.setName("幽默笑话");
		btn33.setType("click");
		btn33.setKey("33");

		ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("菜单");
		mainBtn1.setSub_button(new CommonButton[] { btn11, btn12, btn13 });

		ComplexButton mainBtn2 = new ComplexButton();
		mainBtn2.setName("休闲驿站");
		mainBtn2.setSub_button(new CommonButton[] { btn21, btn22, btn23, btn24, btn25 });

		ComplexButton mainBtn3 = new ComplexButton();
		mainBtn3.setName("更多体验");
		mainBtn3.setSub_button(new CommonButton[] { btn31, btn32, btn33 });

		Menu menu = new Menu();
		menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 });
		return menu;
	}
}
